<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\facade\View; //模板引擎
use app\common\Base;
use think\facade\Request;
use think\exception\ValidateException;
use app\model\GoodsCat;
use app\model\Goods as GoodsMod;
use app\validate\GoodsVal;
use app\model\GoodsSpec;
use app\validate\GoodsSpecVal;

/**
 * @name 普通商品
 */
class Goods extends Base
{
    /**
     * @name 商品列表
     */
    public function list()
    {
        if (request()->isPost()) {
            $param = Request::param();
            $w = '';
            if (isset($param['title']) && $param['title'] != '') {
                $w = "title like '%" . $param['title'] . "%'";
            }
            $list = GoodsMod::list($w, (int)$param['limit'], (int)$param['page']);
            return getBack(0, '', $list['data'], $list['count']);
        }
        return View::fetch();
    }

    /**
     * @name 商品添加
     */
    public function add()
    {
        $cat = GoodsCat::list();
        View::assign('cat', grades($cat));
        if (request()->isPost()) {
            $param = Request::param();
            try { //进入验证
                validate(GoodsVal::class)->check($param);
            } catch (ValidateException $e) {
                return getBack(1, $e->getError());
            }
            if ($param['goods_sn'] == "") {
                $param['goods_sn'] = 'Goods' . uniqid();
            }
            unset($param['file']);
            $res = GoodsMod::add($param);
            if ($res) {
                return getBack(0);
            } else {
                return getBack(2);
            }
        }
        return View::fetch();
    }

    /**
     * @name 商品编辑
     */
    public function edit()
    {
        $cat = GoodsCat::list();
        View::assign('cat', grades($cat));
        if (request()->isGet()) {
            $id = Request::param('id');
            $find = GoodsMod::find($id);
            $find['goods_img_arr'] = explode(',', $find['goods_img']);
            View::assign('find', $find);
        }
        if (request()->isPost()) {
            $param = Request::param();
            try { //进入验证
                validate(GoodsVal::class)->check($param);
            } catch (ValidateException $e) {
                return getBack(1, $e->getError());
            }
            if ($param['goods_sn'] == "") {
                $param['goods_sn'] = 'Goods' . uniqid();
            }
            unset($param['file']);
            $res = GoodsMod::edit($param);
            if ($res) {
                return getBack(0);
            } else {
                return getBack(2);
            }
        }
        return View::fetch();
    }

    /**
     * @name 商品规格
     */
    public function spec()
    {
        if (request()->isGet()) {
            $id = Request::param('id');
            $list = GoodsSpec::list($id);
            View::assign('list', $list);
        }
        if (request()->isPost()) {
            $param = Request::param();
            $id = $param['id'];
            $data = $param;
            unset($data['id']);
            if (!$data) {
                $res = GoodsSpec::del($id);
                if ($res) {
                    return getBack(0);
                } else {
                    return getBack(2);
                }
            } else {
                foreach ($data['data'] as $k => $v) {
                    $v['good_id'] = $id;
                    $arr[] = $v;
                }
                $res = GoodsSpec::edit($id, $arr);
                if ($res) {
                    return getBack(0);
                } else {
                    return getBack(2);
                }
            }
        }
        return View::fetch();
    }

    /**
     * @name 商品删除
     */
    public function del()
    {
        if (request()->isGet()) {
            $id = Request::param('id');
            $res = GoodsMod::del($id);
            if ($res) {
                return getBack(0);
            } else {
                return getBack(2);
            }
        }
    }
}
